package com.ruoyi.admin.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.admin.domain.*;
import com.ruoyi.admin.mapper.BedMapper;
import com.ruoyi.admin.mapper.DepartmentMapper;
import com.ruoyi.admin.mapper.HospitalMapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.OrderMapper;
import com.ruoyi.admin.service.IOrderService;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-23
 */
@Service
public class OrderServiceImpl implements IOrderService
{
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private HospitalMapper hospitalMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public Order selectOrderByOrderId(Long orderId)
    {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param order 订单管理
     * @return 订单管理
     */
    @Override
    public List<Order> selectOrderList(Order order)
    {
        return orderMapper.selectOrderList(order);
    }

    @Override
    public List<OrderVO> selectOrderVOList(List<Order> list) {
        List<OrderVO> orderVOList = new ArrayList<OrderVO>();
        Iterator<Order> it = list.iterator();
        while(it.hasNext()){
            Order order = it.next();
            Bed bed = bedMapper.selectBedByBedId(order.getBedId());
            Hospital hospital = hospitalMapper.selectHospitalByHospitalId(bed.getHospitalId());
            Department department = departmentMapper.selectDepartmentByDeptId(bed.getDeptId());
            OrderVO orderVO = new OrderVO();
            orderVO.setYiyuan(hospital.getHospitalName());
            orderVO.setKeshi(department.getDepartmentName());
            orderVO.setOrderId(order.getOrderId());
            orderVO.setOrderNumber(order.getOrderNumber());
            orderVO.setOrderStatus(order.getOrderStatus());
            orderVO.setAbnormality(order.getAbnormality());
            orderVO.setOrderTime(order.getOrderTime());
            orderVO.setGiveBackTime(order.getGiveBackTime());
            orderVO.setBedId(order.getBedId());
            orderVO.setBedCode(bed.getBedCode());
            orderVO.setLoginId(order.getLoginId());
            orderVO.setLeaseTime(order.getLeaseTime());
            orderVO.setStatus(order.getStatus());
            orderVO.setRent(order.getRent());
            orderVO.setPaymentTime(order.getPaymentTime());
            /*用户电话号码和用户姓名怎么给vo呢*/
            MyVO myVO = orderMapper.selectMyVO(order.getLoginId());
            orderVO.setShoujihao(myVO.getTel());
            orderVO.setNickname(myVO.getNackename());
            orderVOList.add(orderVO);
        }
        Long admin = SecurityUtils.getUserId();

        return orderVOList;
    }

    /**
     * 新增订单管理
     *
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int insertOrder(Order order)
    {
        return orderMapper.insertOrder(order);
    }

    /**
     * 修改订单管理
     *
     * @param order 订单管理
     * @return 结果
     */
    @Override
    public int updateOrder(Order order)
    {
        return orderMapper.updateOrder(order);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderIds(Long[] orderIds)
    {
        return orderMapper.deleteOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteOrderByOrderId(Long orderId)
    {
        return orderMapper.deleteOrderByOrderId(orderId);
    }
}